<script>
/* 对象 */
var person = {
    name: "David",
    age: 28,
    job: "Software Engineer",
    sayName: function() {
        console.log(this.name);
    }
}
person.sayName(); // David

/* 类和对象 */
function Person(name, age, job) {
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = function() {
        console.log(this.name);
    };
}
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");
person1.sayName(); // Nicholas
console.log(person2.age); // 27

/* 原型模式 */
function NewPerson() {
}
NewPerson.prototype = {
    constructor: Person,
    name : "Nicholas",
    age : 29,
    job : "Software Engineer",
    sayName : function () {
        console.log(this.name);
    }
};
var newPerson = new NewPerson();
newPerson.sayName(); // Nicholas

/* 私有变量和函数 */
function MyObject() {
    var privateVariable = 10;

    function privateFunction() {
        return privateVariable;
    }

    this.publicMethod = function() {
        privateVariable ++;
        return privateFunction();
    }
}
var myObject = new MyObject();
console.log(myObject.publicMethod()); // 11
</script>